﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace mevsukovGames
{
    public class InputHandlerAsync: InputHandlerBase
    {
        private bool _hasBeenStrated = false;

        public override void QueryKeyBoard()
        {
            if (_hasBeenStrated)
                return;

            try
            {
                ThreadPool.QueueUserWorkItem(WorkItem);
            }
            finally
            {
                _hasBeenStrated = true;
            }
        }

        private void WorkItem(object obj)
        {
            while (true)
            {
                ConsoleKeyInfo kInfo = Console.ReadKey(true);

                switch (kInfo.Key)
                {
                    case ConsoleKey.LeftArrow:
                        OnMoveLeft();
                        break;
                    case ConsoleKey.RightArrow:
                        OnMoveRight();
                        break;
                    case ConsoleKey.DownArrow:
                        OnMoveDown();
                        break;
                    case ConsoleKey.UpArrow:
                        OnRotate();
                        break;
                }
            }
        }
    }
}
